package com.jy.appmarket.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.util.Pair;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;

import com.jy.appmarket.R;
import com.jy.appmarket.fragment.FragmentLogin;
import com.jy.appmarket.fragment.FragmentRegister;
import com.jy.appmarket.view.PagerSlidingTabStrip;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;

import java.util.ArrayList;
import java.util.List;

public class LoginActivity extends FragmentActivity implements OnClickListener{

	private ViewPager mViewPager;
	private PagerSlidingTabStrip mTabStrip;
	private Fragment mFragmentLogin;
	private Fragment mFragmentRegister;
	private UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.login");
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		initViewPager();
		initTabStrip();
		initViews();
		
		// 设置新浪SSO
		mController.getConfig().setSsoHandler(new SinaSsoHandler());
	}

	private void initViewPager() {
		mViewPager = findViewById(R.id.login_view_pager);
		List<Pair<Fragment, String>> fragments = new ArrayList<Pair<Fragment, String>>();

		mFragmentLogin = new FragmentLogin();
		mFragmentRegister = new FragmentRegister();

		fragments.add(new Pair<Fragment, String>(mFragmentLogin, "登录"));
		fragments.add(new Pair<Fragment, String>(mFragmentRegister, "注册"));
		// cache all fragments
		mViewPager.setOffscreenPageLimit(fragments.size());
		mViewPager.setAdapter(new MyAdapter(getSupportFragmentManager(), fragments));
	}
	
	private void initTabStrip() {
		mTabStrip = findViewById(R.id.activity_login_tabstrip);
		mTabStrip.setViewPager(mViewPager);
		setTabsValue();
	}
	
	private void initViews() {
		findViewById(R.id.finish_btn).setOnClickListener(this);
	}
	
	/**
	 * 对PagerSlidingTabStrip的各项属性进行赋值。
	 */
	private void setTabsValue() {
		DisplayMetrics dm = getResources().getDisplayMetrics();
		// 设置Tab是自动填充满屏幕的
		mTabStrip.setShouldExpand(false);
		mTabStrip.setEnableDivider(false);
		// 设置Tab底部线的高度
		mTabStrip.setUnderlineHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
				0, dm));
		// 设置Tab Indicator的高度
		mTabStrip.setIndicatorHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
				3, dm));
		// 设置Tab标题文字的大小
		mTabStrip.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, dm));
		// 设置Tab Indicator的颜色
		mTabStrip.setIndicatorColor(Color.parseColor("#00ADFF"));
		// 设置选中Tab文字的颜色 (这是我自定义的一个方法)
		mTabStrip.setSelectedTextColor(Color.parseColor("#00ADFF"));
		// 取消点击Tab时的背景色
		mTabStrip.setTabBackground(0);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.finish_btn:
			finish();
			break;
		default:
			break;
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
	
	@Override
	public void finish() {
		super.finish();
		overridePendingTransition(0, R.anim.bottom_out);
	}
	
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		/**使用SSO授权必须添加如下代码(必须在Activity中) */  
        UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode);
        if (ssoHandler != null) {
            ssoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}



	private static class MyAdapter extends FragmentPagerAdapter {

		private List<Pair<Fragment, String>> mFragments;

		public MyAdapter(FragmentManager fm, List<Pair<Fragment, String>> fragments) {
			super(fm);
			mFragments = fragments;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return mFragments.get(position).second;
		}

		@Override
		public int getCount() {
			return mFragments.size();
		}

		@Override
		public Fragment getItem(int position) {
			return mFragments.get(position).first;
		}
	}
	
	
}
